package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.OverflowLog;
import com.sjlh.centurylianhua.mapper.OverflowMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/overflowlog")
public class OverflowController {

    @Autowired
    private OverflowMapper overflow;

    @RequestMapping("/all")
    public ResultData getAll(@RequestBody SearchCon sc){
// 查询所有
        QueryWrapper<OverflowLog> wra=new QueryWrapper<OverflowLog>();
        wra.like(StringUtils.isnotNull(sc.getName()),"user.userName",sc.getName());
        wra.eq(sc.getTypeName2()!=null,"ov.depotId",sc.getTypeName2());
        wra.eq(sc.getTypeName3()!=null,"ov.productId",sc.getTypeName3());
        wra.ge(StringUtils.isnotNull(sc.getBeginDate()),"ov.createTime",sc.getBeginDate());
        wra.le(StringUtils.isnotNull(sc.getEndDate()),"ov.createTime",sc.getEndDate());
        wra.apply("user.id = ov.userId and product.id=ov.productId and depot.id=ov.depotId");
        //封装分页对象
        if(sc.getPage()!=null) {
            Page<OverflowLog> pa=new Page<OverflowLog>(sc.getPage(),sc.getLimit());
            List<OverflowLog> uspa=overflow.getInfo(pa,wra);
            return new ResultData(true,20000," " ,uspa,pa.getTotal());
        }else{
            Long count= Long.valueOf(overflow.selectCount(wra));
            return new ResultData(true,20000," " ,overflow.getInfo(wra),count);
        }
    }
    @RequestMapping("/add")
    public ResultData add(@RequestBody OverflowLog over){
        if(over!=null&& !"".equals(over) &&over.getUserId()!=null){
            System.out.println(over);
            int r = overflow.insert(over);
                return new ResultData(true, 20000, "数据提交成功!", true, null);
            }else{
        return new ResultData(false,20000,"数据不能为空!" ,false,null);
        }
    }
}
